package com.dq.controller.op.opboss.message;

import com.dq.annotation.aop.SysLog;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.NoticeMessageQuery;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.dq.domain.message.oneself.Message;
import com.dq.domain.message.oneself.NoticeMessage;
import com.dq.domain.message.oneself.SubscribeMessage;
import com.dq.service.AllService;
import com.dq.utils.BeanNullUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author cf
 */
@Api(description = "boss发布 通知消息")
@RestController
@RequestMapping("sys/boss/noticeMessage/")
public class BossNoticeMessageController extends AllService {
    /** --------------------------------------------------- 通知消息 --------------------------------------------------- **/
    @ApiOperation("通知消息列表")
    @PostMapping("/list")
    @RequiresPermissions("sys:noticeMessage:list")
    public Vo list(@ApiParam("caseUserId: 关联用户ID") @RequestBody NoticeMessageQuery noticeMessageQuery) throws AdminException {
        noticeMessageQuery.format();
        Map<String, Object> query = noticeMessageQuery.parseMap();
        if(noticeMessageQuery.getCaseUserId() != null) {
            query.put("caseUser_id", noticeMessageQuery.getCaseUserId());
            query.remove("caseUserId");
        }

        List<NoticeMessage> list = dao.find(dao.query2Hql(NoticeMessage.class, query), query);
        Long count = dao.count(dao.count2Hql(NoticeMessage.class, query), query);
        PageHelper.startPage(noticeMessageQuery.getPage(), noticeMessageQuery.getSize());
        PageInfo pageInfo = new PageInfo((List<NoticeMessage.Vo>) VO.toVOCollection(list));
        pageInfo.setTotal(count);

        return new Vo(pageInfo);
    }

    @ApiOperation("消息批量/单个添加")
    @PostMapping("/save")
    @RequiresPermissions("sys:noticeMessage:save")
    public Vo list(@ApiParam("caseUserIds: 消息接受者 id集合") @RequestBody NoticeMessage noticeMessage) throws AdminException {

        // 发送给员工
        noticeMessage.setUserType(SubscribeMessage.UserType.staff);
        noticeMessageService.saveList(noticeMessage, noticeMessage.getCaseUserIds(), Message.MessageType.notice);

        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    @RequiresPermissions("sys:noticeMessage:update")
    public Vo update(@RequestBody @ApiParam(value = "caseUserIds: 员工/顾客 id集合") NoticeMessage noticeMessage) throws AdminException {
        if(noticeMessage.getId() == null) {
            throw new AdminException("缺参id");
        }

        NoticeMessage orgPositionQuery = dao.get(NoticeMessage.class, noticeMessage.getId());
        if (orgPositionQuery == null) {
            throw new AdminException("无对应id对象");
        }
        BeanUtils.copyProperties(noticeMessage, orgPositionQuery, BeanNullUtil.getNullPropertyNames(noticeMessage));
        noticeMessageService.update(orgPositionQuery);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("删除")
    @SysLog("删除")
    @PostMapping("/delete/{id}")
    @RequiresPermissions("sys:noticeMessage:delete")
    public Vo delete(@PathVariable @ApiParam(value = "id") Long id) throws AdminException {
        NoticeMessage orgPositionQuery = dao.get(NoticeMessage.class, id);
        if (orgPositionQuery == null) {
            throw new AdminException("无对应id对象");
        }

        orgPositionQuery.setDeleted(true);
        noticeMessageService.update(orgPositionQuery);
        return new Vo(HttpStatus.OK);
    }
    /** --------------------------------------------------- 通知消息 --------------------------------------------------- **/
}
